package com.yz.android_baozou.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.yz.android_baozou.R;
import com.yz.android_baozou.adapter.Rank_Deatil_RecyclerAdapter;
import com.yz.android_baozou.bean.RankBean;
import com.yz.android_baozou.decoration.SpacesItemDecoration;
import com.yz.android_baozou.helper.OkhttpHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/2.
 */

public class Rank_Agree_Fragment extends Fragment {
    @Bind(R.id.recyclerView_Rank_main)
    RecyclerView recyclerViewRankMain;
    @Bind(R.id.scoreView_Rank)
    PullToRefreshScrollView scoreViewRank;
    //声明投票的网址
    public String PATH_AGREE = "http://dailyapi.ibaozou.com/api/v31/rank/vote/day";
    //声明上下文
    private Context mContext;
    //声明实体类
    private RankBean rankBean;
    //声明实体类下的集合
    private List<RankBean.DataBean> mList_RankBean;
    //声明排行榜的适配器
    private Rank_Deatil_RecyclerAdapter recyclerAdapter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mList_RankBean = new ArrayList<>();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_detial_rank, container, false);
        ButterKnife.bind(this, view);
        setHasOptionsMenu(true);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //加载网络数据
        loadDataFromNet();
        //pulltorefresh设置刷新模式 只能在下拉时刷新
        scoreViewRank.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        //设置下拉刷新
        scoreViewRank.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                loadDataFromNet();
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置RecyclerView的中间的间距
        //使用线性布局管理器
        //设置RecyclerView的布局管理器
        //设置RecyclerView固定行高
        //创建适配器
        //设置RecyclerView的适配器
        recyclerViewRankMain.addItemDecoration(new SpacesItemDecoration(10));
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerViewRankMain.setLayoutManager(manager);
        recyclerViewRankMain.setHasFixedSize(true);
        recyclerAdapter = new Rank_Deatil_RecyclerAdapter(mContext, mList_RankBean,recyclerViewRankMain);
        recyclerViewRankMain.setAdapter(recyclerAdapter);
    }

    private void loadDataFromNet() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //调用Okhttp解析网络数据
                String json = OkhttpHelper.getStringFromUrl(PATH_AGREE);
                //将网络数据解析为实体类
                rankBean = parseJsonToBean(json);
                if (rankBean != null) {
                    mList_RankBean = rankBean.getData();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            recyclerAdapter.reloadRecyclerView(mList_RankBean, true);
                            scoreViewRank.onRefreshComplete();
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public RankBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<RankBean>() {
        }.getType());
    }
    @Override
    //设置自己创建的菜单栏
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //清空原来的菜单栏
        menu.clear();
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_rank,menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //当菜单栏被点击时 根据点击的条目不同更改其所访问的网址
        //同时进行网络加载 刷新页面
        switch(item.getItemId()){
            case R.id.action_today:
                PATH_AGREE="http://dailyapi.ibaozou.com/api/v31/rank/vote/day";
                Toast.makeText(mContext, "正在刷新排行", Toast.LENGTH_SHORT).show();
                loadDataFromNet();
                break;
            case R.id.action_week:
                PATH_AGREE="http://dailyapi.ibaozou.com/api/v31/rank/vote/week";
                Toast.makeText(mContext, "正在刷新排行", Toast.LENGTH_SHORT).show();
                loadDataFromNet();
                break;
            case R.id.action_month:
                PATH_AGREE="http://dailyapi.ibaozou.com/api/v31/rank/vote/month";
                Toast.makeText(mContext, "正在刷新排行", Toast.LENGTH_SHORT).show();
                loadDataFromNet();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
